package twitter;



import utils.Pair;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

public class TwitterUri {
    static final String API_URL = "http://api.twitter.com/1/%s.json";
    static final String API_PART = "/1/%s.json";

    static URI createApiUri(String s) {
        return URI.create(String.format(API_URL, s));
    }

    static URI createApiUri(String s, List<Pair<String, String>> params) {
        String base = String.format(API_URL, s);
        try {
            String path = String.format(API_PART, s);
            String query = null;
            if (!params.isEmpty()) {
                StringBuilder builder = new StringBuilder();
                for (Pair<String, String> p : params) {
                    String param = p.first + "=" + p.second + "&";
                    builder.append(param);
                }
                builder.deleteCharAt(builder.length() - 1);
                query = builder.toString();
            }

            URI twitterUri = new URI("http", "api.twitter.com", path, query, null);
            System.out.println(twitterUri);
            return twitterUri;
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }


        return URI.create(base);
    }


}
